#!/usr/bin/env python

'''
    Create a spreadsheet of all the focus areas

    Take a dump of all reviews and generate a spreadsheet of the content

    % focusAreas.py ReviewsFullList.csv

'''

import sys, os, csv, time

OFILE = time.strftime('focus-%Y.csv')

Q_SUBMISSION = 3
Q_TITLE = 4
Q_NUMBER = 5
Q_QUEST = 6
Q_ANSWER = 7

# The areas can be found in the review XML downloaded from the SIS
AREAS = [
    'N/A',                      # 1
    'Art',                      # 2
    'Education',                # 3
    'First Timer',              # 4
    'Production Communities',   # 5
    'Research',                 # 6
    'Students'                  # 7
]

# Class to store submission information
class Submission:
    def __init__(self, id, title):
        self.Id = id
        self.Title = title
        self.Counts = [0] * len(AREAS)
        self.Total = 0

    def add(self, choice):
        self.Counts[choice-1] += 1      # Choices are 1 based, so subtract 1
        self.Total += 1

    def dump(self, fp):
        row = [ self.Id, self.Title ]
        inv = 0
        if self.Total:
            inv = 1.0/self.Total
        for i in self.Counts:
            if i:
                row.append( i * inv )
            else:
                row.append('')
        fp.writerow(row)

SUBMISSIONS = {}

# Read the csv file passed in.
for row in csv.reader(open(sys.argv[1], 'r')):
    if row[Q_NUMBER] != '9':
        continue
    id = row[Q_SUBMISSION]
    sub = SUBMISSIONS.get(id, None)
    if not sub:
        sub = Submission(id, row[Q_TITLE])
        SUBMISSIONS[id] = sub
    if row[Q_ANSWER]:
        sub.add(int(row[Q_ANSWER]))

# Write out the focus area spreadsheet
fp = csv.writer(open(OFILE, 'w'))
fp.writerow( [ 'ID', 'Title'] + AREAS )
for s in SUBMISSIONS:
    SUBMISSIONS[s].dump(fp)
